if orders and not orders.GetStop then


dofile (ScriptsRootPath .. "lib\\orders.stop.price.lua")
dofile (ScriptsRootPath .. "lib\\orders.stop.qty.lua")
dofile (ScriptsRootPath .. "lib\\orders.stop.active.lua")
dofile (ScriptsRootPath .. "lib\\orders.stop.activated.lua")
dofile (ScriptsRootPath .. "lib\\orders.stop.even.lua")
dofile (ScriptsRootPath .. "lib\\orders.stop.take_limit.lua")


function orders:IsStopOrder(order)
	if self:IsOrder(order)
		and order.condition
		and order.condition_price
	then
		return order
	end
	--orders.log("IsOrder false", lib:FormatDate(), order)
end


function orders:GetStop(order_num)
	if order_num and order_num > 0 then
		local limits = self:GetAllStops()
		
		return limits[order_num]
	end
end


function orders:GetAllStops(sec_code, sort_field, filter)
	local list = {}
	local instrument = lib:GetInstrument(sec_code)
	local o
	
	for i = getNumberOf("stop_orders") - 1, 0, -1 do
		o = getItem("stop_orders", i)
		if (not sec_code or instrument and o.sec_code == instrument.code)
			and self:IsOrder(o)
			and bit.band(o.flags, 3) == 1	-- active
			and (o.stop_order_type == 7		-- activated stop
			or o.stop_order_type == 1		-- stop
			or o.stop_order_type == 10		-- activated t&s
			or o.stop_order_type == 9)		-- t&s
		then
			if not filter or filter(o) then
				if sort_field then
					table.insert(list, o)
				else
					list[o.order_num] = o
				end
			end
		end
	end
	
	if sort_field and #list > 1 then
		if type(sort_field) == "string" then
			local inc
			if string.sub(sort_field, 1, 1) ~= "-" then
				inc = true
			else
				sort_field = string.sub(sort_field, 2, -1)
			end
			table.sort(list,
				function(small, big)
					if inc then
						return small[sort_field] < big[sort_field]
					else
						return small[sort_field] > big[sort_field]
					end
				end
			)
		elseif type(sort_field) == "function" then
			sort_field(list)
		end
	end
	
	--self.log("GetAllStops", list)
	
	return list
end


--[[
function orders:ExecuteStopOrder(order)
	local transaction = self:NewTransaction(order, {
		ACTION = "NEW_ORDER",
		TYPE = "M",
		PRICE = "0",
	})

	if order.flags then
		if bit.test(order.flags, 2) then
			transaction.OPERATION = "S"
		else
			transaction.OPERATION = "B"
		end
	elseif order.ex__ and order.ex__.flags then
		if bit.test(order.ex__.flags, 2) then
			transaction.OPERATION = "S"
		else
			transaction.OPERATION = "B"
		end
	end
	
	self.log("ExecuteStopOrder")
		
	if not self:sendTransaction(transaction, self:KillStopTransaction(order)) then
		--processor:log("ExecuteStopOrder error")
		return
	end
	return true
end
--]]

function orders:ExecuteStopOrder(order)
	local flags
	if order.flags then
		flags = order.flags
	elseif order.ex__ and order.ex__.flags then
		flags = order.ex__.flags
	end
	if flags then
		if bit.test(flags, 2) then
			order.side__ = "S"
		else
			order.side__ = "B"
		end
	end
	
	order.PRICE = nil		-- by market
	--order.PRICE = 0		-- by spread
	
	self.log("ExecuteStopOrder")
	
	return self:SendLimit(order, self:KillStopTransaction(order), self:KillOrderTransaction(self:GetTakeLimit(order)))
	--if not self:SendLimit(order, self:KillStopTransaction(order), self:KillOrderTransaction(self:GetTakeLimit(order))) then
	--	--processor:log("ExecuteStopOrder error")
	--	return
	--end
	--return true
end


function orders:GetPosition(instrument, position_type)	-- nil - paper only, false - limits only, true - both
	local state, side
	
	local positions = {}
	local limits = {}
	local both = {}
	
	for _, order in ipairs(self:GetAllStops(instrument, "sec_code")) do
		state = bit.band(order.flags, 35)
		side = bit.band(order.flags, 4)
				
		if state == 1 then			-- active stop
			if not positions[order.sec_code] then positions[order.sec_code] = 0 end
			
			if side == 4 then		-- sell order - long position
				positions[order.sec_code] = positions[order.sec_code] + int(order.qty)
			else					-- buy order - short position
				positions[order.sec_code] = positions[order.sec_code] - int(order.qty)
			end
		end
				
		if state == 33 then			-- activated stop
			if not limits[order.sec_code] then limits[order.sec_code] = 0 end
			
			if side == 4 then		-- sell order - long position
				limits[order.sec_code] = limits[order.sec_code] + int(order.qty)
			else					-- buy order - short position
				limits[order.sec_code] = limits[order.sec_code] - int(order.qty)
			end
		end
				
		if state == 33 or state == 1 then
			if not both[order.sec_code] then both[order.sec_code] = 0 end
			
			if side == 4 then		-- sell order - long position
				both[order.sec_code] = both[order.sec_code] + int(order.qty)
			else					-- buy order - short position
				both[order.sec_code] = both[order.sec_code] - int(order.qty)
			end
		end
	end
	
	if position_type == nil then		-- paper only
		if instrument then
			return positions[instrument.code or instrument] or 0
					, limits[instrument.code or instrument] or 0
		else
			return positions, limits
		end
	elseif position_type then			-- both
		if instrument then
			return both[instrument.code or instrument] or 0
					, positions[instrument.code or instrument] or 0
					, limits[instrument.code or instrument] or 0
		else
			return both, positions, limits
		end
	else								-- limits only
		if instrument then
			return limits[instrument.code or instrument] or 0
					, positions[instrument.code or instrument] or 0
		else
			return limits, positions
		end
	end
end


function orders:IsPositionLong(order)
	if self:IsStopOrder(order) then
		return bit.test(order.flags, 2)		-- Stop order flags
	end
end


end -- of file
